<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <script src="./js/jquery-3.3.1.min.js"></script>
  <title>Document</title>
  <style>
  .message_board{
    background-color:skyblue;
    width: 500px;
  }
  form {
    border-bottom: 5px solid seagreen
  }
  #name {
    display: block;
  }
  span {
    margin-right: 10px;
  }
  p {
    margin-left: 50px
  }
  </style>
</head>
<body>
  <div class="message_board">
    <form>
      <label for="name">昵称：</label>
      <input type="text" id="name">
      <textarea id="message" cols="50" rows="15"></textarea>
      <input type="checkbox">匿名
      <input type="button" value="发表">
    </form>
    <div id="leave_message">
    </div>
  </div>
  <script>
    $("input:button").click(function() {
      var name = $("#name").val();
      var message = $("#message").val();
      var time = new Date().toLocaleTimeString();
      if(name == "" || message == "") {
        alert("昵称和内容不能为空！");
        return;
      }
      if($("input:checkbox").is(":checked")) {
        name = "路人甲";
      }
      // 获取父元素
      var fa = $("#leave_message");
      // 生成新的节点
      var m_name = $("<span></span>");
      var m_time = $("<span></span>");
      var m_leave = $("<p></p>");
      m_name.text(name);
      m_time.text(time);
      m_leave.text(message);
      fa.append(m_name);
      fa.append(m_time); 
      fa.append(m_leave);
    })
  </script>
</body>
</html>